#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h> 
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "stdNetWork.h"
#include "commonTool.h"
#include "businessFunc.h"
#include "stdShared.h"
#include "pthread.h"
#include <termios.h>
#include <ctype.h>


#define SERVER_PORT 8080
#define SERVER_IP "172.23.194.60"


#define BUFFER_SIZEMSG 128


//int g_running = 1;

/* 联网的通信句柄 */
int g_connfd = 0;



/* 客户端线程接收服务器的信息 */
static void * thread_recvBusiness(void * arg)
{
    /* 通信 */
    int connfd = *(int *)arg;

    while (1)
    {
        /* 服务器接收客户端的信息, 并做出反馈【提示】 */
        
        char readBuffer[BUFFER_SIZEMSG] = { 0 };
        int readBytes = 0;

        while (1)
        {
            readBytes = read(connfd, (void *)&readBuffer, sizeof(readBuffer) - 1);
            if (readBytes <= 0)
            {
                /* 读出错 */
                perror("read error");
                break;
            }
            else
            {
                printf("readBuffer:%s\n", readBuffer);

                enum FUNCTION_OPTION choice = EXITS;
                parseBusinessOption(readBuffer, &choice);
                switch (choice)
                {
                case REGIS:
                    {
                        REGIS_CODE code;
                        /* 客户端接收服务器注册回复信息 */
                        chatRoomRecvServerRegisterInfo(readBuffer, &code);
                    }
                    break;
                case LOGIN:
                    {
                        LOGIN_CODE code;
                        /* 客户端接收服务器登陆回复信息 */
                        chatRoomRecvServerLoginInfo(readBuffer, &code);

                        /* 发个信号 */
                        //pthread_cond_signal();
                    }
                    break;
                case PRI_CHAT:
                    {
                        PRICHAT_CODE code;
                        /* 客户端接收服务器私聊回复信息 */
                        chatRoomRecvServerPriChatInfo(readBuffer, &code);
                    }
                    break;
                case GROUP_CHAT:
                    {
                        GROUPCHAT_CODE code;
                        /* 客户端接收服务器群聊回复信息 */
                        chatRoomRecvServerSharedChatInfo(readBuffer, &code);
                    }
                    break;
                case HEART_CHECK:
                    {
                        /* 客户端接收服务器 心跳回复信息 */
                        chatRoomRecvServerHeartBeatInfo(readBuffer);
                    }
                    break;
                case GOLIVE_NOTIFY:
                    {
                        /* 客户端接收服务器 好友上线回复信息 */
                        chatRoomRecvServerFriendOnlineInfo(readBuffer);
                    }
                    break;
                default:
                    break;
                }
            }
        }
    }

}


int main()
{
    /* 联网 */
    //g_connfd = accessNetwork(SERVER_IP, SERVER_PORT);
    /* 初始高亮选项为注册 */
    int optionLight = REGIS; 
    chatRoomUiMenu11(optionLight);

      
    return 0;
}
